﻿//选项列隐藏与现实配置
var hidecompatchcolume = true;

function IsWhat(value){
    if(IsChinese(value))    
        return 0;
    else if(IsNumber(value))   
        return 1;
    else   
        return 2;
}

function IsChinese(value){
    value = value.charAt(0);
    var escape_v = escape(value);
    if(escape_v.length==(value.length)*6 || value=="\r" || value=="\n")
        return true;
    else
        return false;
}

function IsNumber(value){
    var num = parseInt(value);     
    if(num==0 || num)
        return true;
    else
        return false;
}

function Trim(value){
    return value.replace(/^\s+|\s+$/g,"");   
}

var isIE6 = document.all && ([/MSIE (\d)\.0/i.exec(navigator.userAgent.toLowerCase())][0][1] == 6);

function Bind(object, fun) {
    var args = Array.prototype.slice.call(arguments).slice(2);
    return function() {
        return fun.apply(object, args.concat(Array.prototype.slice.call(arguments)));
    }
}

function BindAsEventListener(object, fun) {
    var args = Array.prototype.slice.call(arguments).slice(2);
    return function(e) {
        return fun.call(object, (e || window.event), args);
    }
}
    
window.attachEventHandle = function(elem, eventName, func){
    if(window.attachEvent){
        elem.attachEvent('on' + eventName, func);
    }else if(window.addEventListener){
        elem.addEventListener(eventName, func, false);
    }
};

window.detachEventHandle = function(elem, eventName, func){
    if(window.detachEvent){
        elem.detachEvent('on' + eventName, func);
    }else if(window.removeEventListener){
        elem.removeEventListener(eventName, func, false);
    }
};

function getPosition(elem){
	var x = 0, y = 0;
	while(elem) {
		x += elem.offsetLeft || 0;
		y += elem.offsetTop || 0;
		elem = elem.offsetParent;
	}
	return {x:x, y:y};
}

function JumptoFund() {
	var sKeyword = document.getElementById("sKeyWord");
	if (sKeyword.value == "代码/名称/拼音/别名") {
		alert("请在输入框中选择基金");
		return;
	}
	var code = sKeyword.getAttribute("code");
	if(code!=null&&code!=""){
		window.open("http://fund.fund123.cn/html/"+code);
		}
	else{
		alert("请输入正确的基金信息");
	}
}
	
function searchPicker(txtEnterID, dataArray, pickerType, changeValue, options){
    this.enterValue = "";    
    this.enterID = txtEnterID;
    this.show_hide = "hide";
    this.focus = true;
    this.Time = true;
    this.Trs = null;
    this.Rows = null;
    this.dataArray = dataArray;
    this.pickerType = pickerType;
    this.fsChangeValue = changeValue;
    this.options = options;
    this.inDrawTable = false;        
    this.timerFsInDrawTable = null;
    this.divHeight = 300;
    if(options){
        if(options.height){
            this.divHeight = parseFloat(options.height);
        }
    }
}

searchPicker.prototype = {
    fsInit : function(){
        this.Dom = document.getElementById(this.enterID);
        this.Div = document.body.insertBefore(document.createElement('div'), document.body.childNodes[0]);
        this.Div.className = 'searchPicker_listDiv';        
        with(this.Div.style){        
            zIndex = 9999999; 
            position = 'absolute';
            left = getPosition(this.Dom).x + 'px';
            top = getPosition(this.Dom).y + this.Dom.offsetHeight + 'px';
        }
        if (hidecompatchcolume) {
            this.Div.style.width = "250px";
        }
        
        attachEventHandle(this.Dom, "focus", Bind(this,this.fsClearValue));
        attachEventHandle(this.Dom, "blur", Bind(this,this.fsBlur));

        attachEventHandle(this.Dom, "keydown", BindAsEventListener(this, this.fsKeyDown));
        attachEventHandle(this.Dom, "keyup", BindAsEventListener(this, this.fsKeyUp));        

        this.timerCheckIsDisplay = window.setInterval(Bind(this, this.checkIsDisplay), 300); //定期检查父窗口是否已经关闭
    },   
    
    checkIsDisplay: function(){
        try{
            var isDisplay = true;
            var el = this.Dom;
            if(this.Div.style.display == 'none'){
                return;
            }
            while(el && el.tagName.toLowerCase() != 'body'){
                el = el.parentNode;
                if(!el || (el.style && el.style.display == 'none')){
                    isDisplay = false;
                    break;
                }
            }
            if(!isDisplay){
                this.fsHideDiv();
                if(this.timerCheckIsDisplay){
                    window.clearInterval(this.timerCheckIsDisplay);
                }
            }
        }catch(e){}
    },
    
    fsClearValue: function(e){
        switch(this.pickerType){
            case "fund":
                if(this.Dom.value == "代码/名称/拼音/别名")
                    this.Dom.value = "";
                break;
            case "stock":
                if(this.Dom.value == "代码/名称/拼音")
                    this.Dom.value = "";
                break;
        }
    },
    
    fsSetDefaultValue: function(){

       switch(this.pickerType){
            case "fund":
                if(this.Dom.value == ""){
                    this.Dom.value = "代码/名称/拼音/别名";
                }
                break;
            case "stock":
                if(this.Dom.value == ""){
                    this.Dom.value = "代码/名称/拼音";
                }
                break;
        }
    },  
    
    fsBlur : function(){
        window.setTimeout(Bind(this, this.fsSetDefaultValue), 100);
    },    

    fsFill : function(code, name){
        this.Dom.title = code;
        this.Dom.setAttribute("code" , code);
        this.Dom.value = name;
        this.Dom.blur(); 
        this.fsHideDiv();        
        this.enterValue = "";
        
        if(this.fsChangeValue){
            this.fsChangeValue();
        }
    },
    
    fsMouseOver : function(e, i){                     
        if(e != "div"){
            this.Trs[this.Rows].style.backgroundColor = "#fff"; // "#f7fafb";
            this.Trs[this.Rows].style.color = "#333";             
            e.style.backgroundColor = "#cedfe9";
            e.style.color = "#333";                  
            if(i == undefined)
                this.Rows = parseInt(e.title.split("|")[2]); 
        }           
        this.show_hide = "show";
    },
    
    fsMouseOut : function(e){        
        if(e != "div"){
            e.style.backgroundColor = "#fff"; //"#f7fafb";
            e.style.color = "#333";            
        }
        this.show_hide = "hide";
    },
    fsKeyDown : function(e){
         if(e.keyCode == 38) { //Up    
            if((this.Rows>0) && this.Rows){    
                var text;
                if(this.Rows-1>0){
                    text = this.Trs[this.Rows-1].innerText || this.Trs[this.Rows-1].textContent; 
                    text = Trim(text); 
                }else{
                    return;
                }
                if(text != "选项代码名称" && text != "选项功能拼音"){
                    this.fsMouseOver(this.Trs[this.Rows-1], 0);
                    this.fsScroll(this.Trs[this.Rows-1], -1, "up");     
                    this.Rows -=1;  
                }else if(this.Rows -2 > 0){                  
                    this.fsMouseOver(this.Trs[this.Rows-2], +2);
                    this.fsScroll(this.Trs[this.Rows-2],-2, "up"); 
                    this.Rows -=2; 
                } 
                this.show_hide = "hide";                        
            }
        }else if(e.keyCode == 40) { //Down  
            if((this.Trs.length > this.Rows) && this.Rows){            
                var text;
                if(this.Rows+1<this.Trs.length){
                    text = this.Trs[this.Rows+1].innerText || this.Trs[this.Rows+1].textContent; 
                    text = Trim(text); 
                }else{
                    return;
                }
                if(text != "选项代码名称" && text  != "选项功能拼音") {
                    this.fsMouseOver(this.Trs[this.Rows+1], 1);
                    this.fsScroll(this.Trs[this.Rows+1], 1, "down");
                    this.Rows +=1;
                }
                else if(this.Trs.length > this.Rows +2){                 
                    this.fsMouseOver(this.Trs[this.Rows+2], -2);
                    this.fsScroll(this.Trs[this.Rows+2], 2, "down");
                    this.Rows +=2;   
                }
                this.show_hide = "hide";
            }            
        }else if(e.keyCode  == 13) {   //enter   
                if(this.show_hide == "show" || this.Dom.title==""){
                var Title = this.Trs[this.Rows].title.split("|");
                if(Title){        
                    this.show_hide = "hide";            
                    this.fsFill(Title[0], Title[1]);                    
                }
            }  
        }
    },
    fsKeyUp : function(e){
        if(e.keyCode == 38 || e.keyCode == 40 || e.keyCode  == 13) { //Up, Dowm, Enter 
            return;
        }else if(e.keyCode == 27){ // esc
            window.event.keyCode=0;   
            window.event.returnValue=false;
        }else{
            this.fsBindSearch();
        }        
    },    
    
    fsScroll : function(e, i, direction){
        var Num;
        var Number;
        if(window.event){
            Num = 25;
        }else{
            Num= 25;
        }
        Number = this.Div.offsetHeight;        
        var range = e.offsetTop - this.Div.scrollTop;   
        if(direction == "up"){
            if(range <= 0)
            {
                this.Div.scrollTop += Num * i;
            }
        }
        else if(direction == "down"){
            if(range + 25 >= Number)
            {            
                this.Div.scrollTop += Num * i;
            }
        }
    },
    
    fsBindSearch : function(){
        var tag = false;
        if( ((this.pickerType == "fund" && this.Dom.value != "代码/名称/拼音/别名")
            || (this.pickerType == "stock" && this.Dom.value != "代码/名称/拼音"))
            && Trim(this.Dom.value) != ""){
            var value = Trim(this.Dom.value);     
            for(var i=0; i<this.dataArray.length; i++){
                if(this.dataArray[i].length >= 2 && this.dataArray[i][0] == value){ 
                    if(this.Dom.getAttribute("code") != this.dataArray[i][1]){      
                        tag = true;  
                        this.Dom.title = this.dataArray[i][1];
                        this.Dom.setAttribute("code" , this.dataArray[i][1]);
                        if(this.fsChangeValue){
                            this.fsChangeValue();
                        }
                    }
                }
            }
        }
        if(!tag){
            if(this.Dom.getAttribute("code") != ""){
                this.Dom.title = "";
                this.Dom.setAttribute("code" , "");
                if(this.fsChangeValue){
                    this.fsChangeValue();
                }
            }
        }
        if( ((this.pickerType == "fund" && this.Dom.value != "代码/名称/拼音/别名")
            || (this.pickerType == "stock" && this.Dom.value != "代码/名称/拼音"))
        && (this.enterValue != this.Dom.value) ){        
            this.Dom.value = Trim(this.Dom.value);
            this.fsGetSearchData(this.Dom.value);
            //this.timerFsInDrawTable = setInterval(Bind(this, this.fsInDrawTable, this.Dom.value), 1000);
            this.enterValue = this.Dom.value;          
        }
    },
    
   
    fsGetSearchData : function(KeyWord){    
           if(!this.inDrawTable){
               this.inDrawTable = true;            
               if(this.pickerType == "fund"){
                    if (KeyWord == '') {
			this.fsGetSearchDataTable(KeyWord, []);
                    } else {
                    	this.fsGetSearchDataTable(KeyWord, this.dataArray);          
		    }
               }else if(KeyWord == ""){
                    this.fsGetSearchDataTable(KeyWord, []);
               }else{
                    Ext.Ajax.request({
                        url:"/AjaxHelper.aspx",
                        params: {
                            type : "GetSuggestStockCodes",
                            key : KeyWord,
                            stockType: this.options.stockType
                        },
                        success : this.fsGetSuggestStockCodesCallBack.createDelegate(this, [KeyWord], true)
                    });
               }       
           }    
    },
    
    fsGetSuggestStockCodesCallBack: function(response, options, KeyWord){
        if(response.responseText.indexOf('[') != -1){
            this.dataArray = eval(response.responseText);
        }else if(response.responseText != ''){
            this.dataArray = [];
        }
        this.fsGetSearchDataTable(KeyWord, this.dataArray);
    },
    
    fsGetSearchDataTable : function(KeyWord, dataArrayList){    
        //try{
            var fundTable = document.createElement('table');
            fundTable.cellpadding = '0';
            fundTable.cellspacing = '0';
            fundTable.width = '100%';
            fundTable.style.width = '100%';
            fundTable.style.borderCollapse = 'collapse';
            fundTable.className = 'searchPicker_titleCss';    
            
            var tbody = document.createElement('tbody');
            fundTable.appendChild(tbody);
            
            var index = IsWhat(KeyWord);        
            var tr1,tr2,tr3;
            if(index ==0){
                tr1 ="40%";tr2 ="20%";tr3 ="40%";
            }
            else{
                tr1 ="30%";tr2 ="30%";tr3 ="40%";
            }
            var Row = 1;
            KeyWord = KeyWord.toLowerCase();             
                    
            Len = dataArrayList.length;
            var sequeace;
            if(Row == 1) sequeace=0;else {sequeace=Row+1; Row +=2;} 
             
            for(var i=0; i<Len; i++){
                var code = dataArrayList[i][1];
                var name = dataArrayList[i][0];
                var Title = code + "|" + name;
                            
                if(index == 2 && (dataArrayList[i][index].split(",")).length >1){
                    var sunArray = dataArrayList[i][index].split(",");
                    for(var z=0; z<sunArray.length; z++){
                        var sunItem = sunArray[z];
                        var sunSubKW = sunItem.substr(0, KeyWord.length);
                        if(sunSubKW == KeyWord && sunSubKW != -1){
                            var select = "<span style='color:Red'>"+sunSubKW+"</span>";
                            select += sunItem.substr(KeyWord.length, sunItem.length - KeyWord.length);
                            var tr = document.createElement('tr');
                            tr.title = Title + "|" + Row;
                            attachEventHandle(tr, 'click', Bind(this, this.fsFill, code, name));
                            attachEventHandle(tr, 'mouseover', Bind(this, this.fsMouseOver, tr));
                            attachEventHandle(tr, 'mouseout', Bind(this, this.fsMouseOut, tr));                        
                            
                            var td = document.createElement("td");
                            td.innerHTML = "&nbsp;"+select;
                            tr.appendChild(td);
                            if (hidecompatchcolume) {
                                td.style.display = "none"; 
                            }
                            
                            var td = document.createElement("td");
                            
                            if (hidecompatchcolume) {
                                td.innerHTML = "&nbsp;" + dataArrayList[i][1];
                            } else {
                                td.innerHTML = dataArrayList[i][1];
                            }
                            
                            tr.appendChild(td);
                            var td = document.createElement("td");
                            td.innerHTML = dataArrayList[i][0];
                            tr.appendChild(td);
                            tbody.appendChild(tr);
                            Row++;
                            break;
                        }
                    }
                }
                else{
                    var item = dataArrayList[i][index];
                    var subKW = item.substr(0, KeyWord.length);
                    if(subKW == KeyWord && subKW != -1) {
                        var select = "<span style='color:Red'>"+subKW+"</span>"
                        select += item.substr(KeyWord.length, item.length - KeyWord.length); 
                        var tr = document.createElement('tr'); 
                        tr.title = Title + "|" + Row;
                        attachEventHandle(tr, 'click', Bind(this, this.fsFill, code, name));
                        attachEventHandle(tr, 'mouseover', Bind(this, this.fsMouseOver, tr));
                        attachEventHandle(tr, 'mouseout', Bind(this, this.fsMouseOut, tr));  
                        var td = document.createElement("td");
                        td.innerHTML = "&nbsp;"+select;
                        tr.appendChild(td);
                        if (hidecompatchcolume) {
                            td.style.display = "none"; //隐藏“选项”列
                        }
                        
                        var td = document.createElement("td");
                        
                        if (hidecompatchcolume) {
                            td.innerHTML = "&nbsp;" + dataArrayList[i][1];
                        } else {
                            td.innerHTML = dataArrayList[i][1];
                        }
                        
                        tr.appendChild(td);
                        var td = document.createElement("td");
                        td.innerHTML = dataArrayList[i][0];
                        tr.appendChild(td);
                        tbody.appendChild(tr);
                        Row++;
                    }
                }
            }
            
            if(tbody.childNodes.length > 0){
                var tr = document.createElement('tr');
                tr.className = "header";
                var td = document.createElement("td");
                td.innerHTML = "&nbsp;选项";
                td.width = tr1;
                tr.appendChild(td);
                if (hidecompatchcolume) {
                    td.style.display = "none"; //隐藏“选项”列
                }
                
                var td = document.createElement("td");
                if (hidecompatchcolume) {
                    td.innerHTML = "&nbsp;代码";
                    td.width = "15%";
                } else {
                    td.innerHTML = "代码";
                    td.width = tr2;
                }
               
                tr.appendChild(td);
                var td = document.createElement("td");
                td.innerHTML = "名称";
                td.width = tr3;
                tr.appendChild(td);
                tbody.insertBefore(tr, tbody.firstChild);
            }
            else{
                fundTable = null;
            }          
             
            if(fundTable){
                this.Div.innerHTML = '';
                this.Div.appendChild(fundTable);
                this.fsShowDiv();
                
                this.Trs = this.Div.getElementsByTagName("TR");                
                this.Rows = 1;
                this.fsMouseOver(this.Trs[this.Rows], 1);  
                this.Div.scrollTop = 0;  
                this.Dom.title = "";                                
                this.show_hide = "hide";
                
                var number;                
                if(window.event)
                    number = this.divHeight;
                else
                    number = this.divHeight + 1; 
                if(this.Div.scrollHeight < number)                    
                    this.Div.style.height = "auto";
                else
                    this.Div.style.height = this.divHeight + "px";
            }
            else
            {
                this.fsHideDiv();
            }
       // }catch(e){};
        this.inDrawTable = false;
    },
    
    fsShowDiv: function(){
        this.Div.style.display = 'block';
        this.Div.style.left = getPosition(this.Dom).x + 'px';
        this.Div.style.top = getPosition(this.Dom).y + this.Dom.offsetHeight + 'px';
        if(isIE6){
            for(var i=0;i<document.getElementsByTagName('select').length;i++){
                document.getElementsByTagName('select')[i].style.display = 'none';
            }
        }
    },
    
    fsHideDiv: function(){
        this.Div.style.display = 'none';
        if(isIE6){
            for(var i=0;i<document.getElementsByTagName('select').length;i++){
                document.getElementsByTagName('select')[i].style.display = '';
            }
        }
   }   
}